package com.example.sinoyd.frameapplication.ProjectCodes.Fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.os.Looper
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.sinoyd.frameapplication.KotlinFrame.Dataclass.Chartdata
import com.example.sinoyd.frameapplication.KotlinFrame.Dataclass.gson
import com.example.sinoyd.frameapplication.KotlinFrame.Frame.Uitl.Networkrequestmodel
import com.example.sinoyd.frameapplication.KotlinFrame.MpChart.CombinedChartDrawing
import com.example.sinoyd.frameapplication.KotlinFrame.Uitl.showdialog
import com.example.sinoyd.frameapplication.KotlinFrame.View.TimeSelectionBarView
import com.example.sinoyd.frameapplication.ProjectCodes.Dataclass.GetAppPortWaterQualityTrend
import com.example.sinoyd.frameapplication.R
import com.sinoyd.Code.Until.Networkrequestaddress
import com.sinoyd.Code.Until.SharedPreferencesFactory
import com.sinoyd.environmentsz.Kotlin.getToday
import com.sinoyd.environmentsz.Kotlin.getlastmonthToday
import com.sinoyd.environmentsz.Kotlin.getlastweekToday
import kotlinx.android.synthetic.main.include_title_layout.*
import kotlinx.android.synthetic.main.trend_analysis_fragment.*
import okhttp3.Response
import org.jetbrains.anko.act
import org.jetbrains.anko.runOnUiThread
import org.jetbrains.anko.toast
import java.util.*

/**
 * 趋势分析界面  水质趋势
 * **/
@SuppressLint("ValidFragment")
class TrendAnalysisFragment(var name: String = "趋势分析") : BaseFragment() {
    var date = Date()
    var strTime = date.getlastweekToday()
    var endTime = date.getToday()
    //选择站点后进行的操作
    override fun selectPortCallBack() {
        getAppPortWaterQualityTrend()
    }


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val conview = inflater.inflate(R.layout.trend_analysis_fragment, null)
        return conview
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setview()
        setlisterens()
        getAppPortWaterQualityTrend()
    }


    //设置界面
    private fun setview() {
        center_title.text = name
        radiobutton1.isChecked = true
        when (SharedPreferencesFactory.getdata(act, "OS")) {
            "air" -> {
                radiobutton1.text = "最近24小时"
                strTime = date.getToday()
                endTime = date.getToday()
            }
            "water" -> {
                radiobutton1.text = "最近7天"
                strTime = date.getlastweekToday()
                endTime = date.getToday()
            }
        }
    }

    //设置各种监听
    private fun setlisterens() {
        //选项卡
        radiogroup.setOnCheckedChangeListener { radioGroup, i ->
            when (i) {
                R.id.radiobutton1 -> {
                    trendtimeselection.visibility = View.GONE
                    when (SharedPreferencesFactory.getdata(act, "OS")) {
                        "air" -> {
                            strTime = date.getToday()
                            endTime = date.getToday()
                        }
                        "water" -> {
                            strTime = date.getlastweekToday()
                            endTime = date.getToday()
                        }
                    }
                    //发请求
                    getAppPortWaterQualityTrend()
                }
                R.id.radiobutton2 -> {
                    trendtimeselection.visibility = View.GONE
                    strTime = date.getlastmonthToday()
                    endTime = date.getToday()
                    //发请求
                    getAppPortWaterQualityTrend()
                }
                R.id.radiobutton3 -> {
                    trendtimeselection.visibility = View.VISIBLE
                    strTime = date.getlastweekToday()
                    endTime = date.getToday()
                    //发请求
                    getAppPortWaterQualityTrend()
                }
            }
        }
        //选择框
        trendselectionbarview.setdefaulttext("曲线图", "柱状图")
                .setdefaultSelectBt(trendselectionbarview.LEFTTRUE)
                .settextcolor("#FFFFFF")
                .setradiisleft(floatArrayOf(10f, 10f, 0f, 0f, 0f, 0f, 10f, 10f))
                .setradiisright(floatArrayOf(0f, 0f, 10f, 10f, 10f, 10f, 0f, 0f))
                .setradiobg("#550f82ff", "#0f82ff")
                .setOnCheckedChangeListener { radioGroup, i ->
                    when (i) {
                        R.id.rb_bt1 -> {
                            TrendCombinedChartleft.visibility = View.VISIBLE
                            TrendCombinedChartright.visibility = View.GONE
                        }
                        R.id.rb_bt2 -> {
                            TrendCombinedChartleft.visibility = View.GONE
                            TrendCombinedChartright.visibility = View.VISIBLE
                        }
                    }
                }
        //时间选择
        trendtimeselection.setdefaulttime(date.getlastweekToday(), date.getToday())
                .setdefaulttextcolor("#000000")
                .setfaultimagecolor("#000000")
                .setTimeSelectionbarlistener(object : TimeSelectionBarView.TimeSelectionbarlistener {
                    override fun timeselectionbardoing(lefttime: String, righttime: String) {
                        strTime = lefttime
                        endTime = righttime
                        getAppPortWaterQualityTrend()
                    }
                }).show()
    }


    //获取水质趋势
    fun getAppPortWaterQualityTrend() {
        showdialog(act, "loadshow")
        var ntrequest = Networkrequestmodel()
        ntrequest.setMethod(Networkrequestmodel.GETREQUEST)
                .settag("getAppPortWaterQualityTrend")
                .seturl(Networkrequestaddress.URL_GetAppPortWaterQualityTrend)
                .addparam("PointId", portid)
                .addparam("strTime", strTime)
                .addparam("endTime", endTime)
                .start(this)
    }

    var getAppPortWaterQualityTrend = GetAppPortWaterQualityTrend()
    var list图表: HashMap<String, ArrayList<Chartdata>> = HashMap()
    override fun requestSuccess(response: Response, TAG: String) {
        super.requestSuccess(response, TAG)
        when (TAG) {
            "getAppPortWaterQualityTrend" -> {
                getAppPortWaterQualityTrend = gson.fromJson(responsestr, GetAppPortWaterQualityTrend::class.java)
                if (getAppPortWaterQualityTrend.data.size > 0) {
                    //整理数据
                    list图表 = HashMap()
                    var list: ArrayList<Chartdata> = ArrayList()
                    for (item in getAppPortWaterQualityTrend.data) {
                        list.add(Chartdata(item.time,
                                when (item.grade) {
                                    "I" -> {
                                        "0"
                                    }
                                    "II" -> {
                                        "1"
                                    }
                                    "III" -> {
                                        "2"
                                    }
                                    "IV" -> {
                                        "3"
                                    }
                                    "V" -> {
                                        "4"
                                    }
                                    "劣V" -> {
                                        "5"
                                    }
                                    else -> {
                                        "0"
                                    }
                                }
                        ))
                    }
                    list图表.put("数据", list)

                    display曲线图()

                    display柱状图()

                } else {
                    Looper.prepare()
                    toast("未获取到趋势分析数据")
                    Looper.loop()
                }
            }
        }
    }


    fun display曲线图() {
        runOnUiThread {
            var ccd = CombinedChartDrawing(act,TrendCombinedChartleft, list图表, CombinedChartDrawing.ONLY_BROKEN_LINE, true, false, false, false)
            ccd.initview().showchart()
        }
    }

    fun display柱状图() {
        runOnUiThread {
            var ccd = CombinedChartDrawing(act,TrendCombinedChartright, list图表, CombinedChartDrawing.MORE_BAR_GRAPH_PORTRAIT, true, false, false, false)
            ccd.initview().showchart()
        }
    }
}